package com.gec.hrm.dao.impl;

import com.gec.hrm.pojo.User;
import com.gec.hrm.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends JDBCUtils<User> implements UserDao {
    /*
     * 用户查询的方法--查询user_inf表中全部信息
     *
     * */
    public List<User>  userList(String loginname, String status,int page,int limit){
        String sql = "select * from user_inf where 1=1 ";//万能条件
        //需要对条件做一下判断，不为空
        //再输入某个条件后，另外一个条件不是null,而是一个空的字符串
        if(loginname !=null && !"".equals(loginname))
        {
            sql+= "and loginname like '%"+loginname+"%'";
        }
        if(status !=null && !"".equals(status))
        {
            sql+= "and status ="+status+"";
        }
        sql+=" limit "+(page-1)*limit +","+limit+"";
        List<User> users = query(sql);
        return users;
    }

    @Override
    public boolean checkName(String loginname) {
        List<User> users= query("select * from user_inf where loginname = ?",loginname);
        if(users.size()>0){
           return true;
        }
        return false;
    }

    @Override
    public int addUser(User user) {
        return update("insert into user_inf  values(null,?,?,?,CURRENT_TIMESTAMP,?)",
                user.getLoginname(), user.getPassword(),user.getStatus(),user.getUsername());
    }

    @Override
    public int updUser(User user) {

        return update("update user_inf set loginname=?,username=?,status=? where id=?",
                user.getLoginname(),user.getUsername(),user.getStatus(),user.getId());
    }

    @Override
    public int count() {
        List<User> users= query("select * from user_inf");
        return users.size();
    }

    @Override
    public int delUser(int id) {
        return update("delete from user_inf where id=?",id);
    }

    @Override
    public User findById(int id) {
         var lists= query("select * from user_inf where id=?",id);
        if(lists.size()<1){
            return null;
        }
        return lists.get(0);
    }

    public User login(String username, String password) {
        //条件的值，先不要赋值，先给？，占位符
        List<User> users = query("select * from user_inf where loginname = ? and password = ?",username,password);
        //假如说账号和密码不对，没有查询出用户，集合是没有任何数据的，不用够通过get方法取出第一条数据返回
        if(users.size()>0)
        {
            return users.get(0);
        }
        System.out.println("账号名："+users.get(0).getLoginname()+"--密码："+password);
        return null;
    }

    public User getBean(ResultSet rs) {
        User user = new User();


        try {
            user.setId(rs.getInt("ID"));
            user.setLoginname(rs.getString("loginname"));
            user.setPassword(rs.getString("PASSWORD"));
            user.setStatus(rs.getInt("STATUS"));
            user.setCreatedate(rs.getDate("createdate"));
            user.setUsername(rs.getString("username"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

}
